import Label from "/label"
import Button from "/button"
import Image from "/image"
export default class UI {
  constructor(love){
    this.love = love
    this.buff = []
    this.font = this.love.graphics.newFont(40)
    this.textColor = "black"
    this.fillColor = "Silver"
    this.frameColor = "Gray"
    this.frameWidth = 1
  }
  newButton(conf){
    let btn = new Button(this.love,conf)
    this.buff.push(btn)
    return btn
  }
  newLabel(conf){
    let label = new Button(this.love, conf)
    this.buff.push(label)
    return label
  }
  newImage(conf){
    let img = new Image(this.love, conf)
    this.buff.push(img)
    return img
  }
  remove(obj){
    obj.destroyed = true
  }
  clear(){
    this.buff = []
  }
  backward(obj){
    for (var i in this.buff) {
      let obj2 = this.buff[i]
      if (obj == obj2){
        this.buff.splice(i,1)
        this.buff.splice(0,0,obj)
        return
      }
    }
  }
  foreward(obj){
    for (var i in this.buff) {
      let obj2 = this.buff[i]
      if (obj == obj2) {
        this.buff.splice(i, 1)
        this.buff.push(obj)
      }
    }
  }
  hide(obj){
    obj.visible = false
  }
  show(obj){
    obj.visible = true
  }
  update(){
    let n = []
    for (var i in this.buff) {
      let obj = this.buff[i]
      if(!obj.destroyed){
        n.push(obj)
      }
    }
    this.buff = n
  }
  onTouch(x,y){
    this.active = false
    for (var i in this.buff) {
      let obj = this.buff[i]
      if (obj.available) {
        if (obj.checkTouch(x, y)) {
          this.active = true
          return
        }
      }
    }
  }
  onHold(x,y,dt){
    this.active = false
    for (var i in this.buff) {
      let obj = this.buff[i]
      if (obj.checkHold(x, y,dt)) {
        this.active = true
        return
      }
    }
  }
  draw(){
    let n = this.buff.length
    for (var i in this.buff) {
      let obj = this.buff[n-i-1]
      if (obj.visible) {
        obj.draw()
      }
    }
  }
  load(sheet){
    for (var i in sheet){
      var t = sheet[i]
      switch(t.type) {
        case "image":
          t.obj = this.newImage(t)
          break
        case "label":
          t.obj = this.newLabel(t)
          break
        case "button":
          t.obj = this.newButton(t)
          break
      }
    }
  }
}